var cgmx = {
    setBtnStatus:function () {
        var row = $('#grid_cgmx').datagrid('getSelected');
    }
}
$(function () {
    //采购方公司加载
    $("[name='sbgs_jc']").jgbmtree($("[name='sbbm_jc']"), null, '', '');
    //供应商加载
    $("#gys_jc").qyjgzcombobox();
    //表格初始加载
    $('#grid_cgmx').datagrid({
        url:$.webPath() + "/cggl/cgmx/cgmxList",
        toolbar:"#toolbar_cgmx",
        fitColumns:false,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        striped:true,
        showFooter:true,
        checkbox:true,
        frozenColumns:[
            [
                {field:'checked', checkbox:true},
                {field:'cgdh', title:'采购单号',align: 'center', width:110},
                {field:'gys_jc', title:'供应商',align: 'center', width:170},
                {field:'ck_mc', title:'仓库',align: 'center', width:70}
            ]
        ],
        columns:[
            [
//                {field:'gg_oldcode', title:'老物资编码',align: 'center', width:100},
                {field:'gg_code', title:'物资编码',align: 'center', width:100},
                {field:'gg_name', title:'品名',align: 'center', width:100},
                {field:'gg_ggxh', title:'规格',align: 'center', width:100},
                {field:'gg_cz', title:'材质',align: 'center', width:100},
                {field:'gg_dw', title:'单位',align: 'center', width:50},
                {field:'sbsl', title:'采购数量',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatSlForHtml(value,',');
                        return val;
                    }},
                {field:'rksl', title:'入库数量',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatSlForHtml(value,',');
                        return val;
                    }},
                {field:'hsj', title:'含税单价(元)',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatJeForHtml(value,',');
                        return val;
                    }
                },
                {field:'hse', title:'含税总金额(元)',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatJeForHtml2(value,',');
                        return val;
                    }
                },
                {field:'bhsj', title:'不含税单价(元)',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatJeForHtml(value,',');
                        return val;
                    }
                },
                {field:'bhse', title:'不含税总金额(元)',align: 'right', width:120,
                    formatter:function (value) {
                        var val = formatJeForHtml2(value,',');
                        return val;
                    }
                },
                {field:'rksj', title:'入库时间',align: 'center', width:70},
                {field:'sbgs_jc', title:'公司名称',align: 'center', width:70},
//                {field:'sbbm_qc', title:'部门名称',align: 'center', width:70},
                {field:'zt', title:'提交状态', width:70, align: 'center',
                    formatter:function (value, rowData, rowIndex) {
                        if (value == 'ybc') {
                            return "已保存";
                        } else if (value == 'ytj') {
                            return "已提交"
                        } else if (value == 'ysp') {
                            return "已审批"
                        }
                    }
                },
                {field:'yrk', title:'入库状态', width:70, align: 'center',
                    formatter:function (value) {
                        if (value == true)
                            return "已入库";
                        else if (value != undefined && value == false)
                            return  "未入库";
                    }
                },
                {field:'zt_yjs', title:'结算状态', width:70,align: 'center',
                    formatter:function (value) {
                        if (value == true)
                            return "已结算";
                        else if (value != undefined && value == false)
                            return "未结算";
                    }
                }
            ]
        ],
        onLoadSuccess:function () {
            cgmx.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function () {
            cgmx.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //查询按钮
    $("#btn_cgmx_search").click(function () {
        cgmx_search();
    });
    //加载仓库
    $('#ck_mc').combobox({
        url:$.webPath() + '/organ/ck/ckmcList',
        valueField:'id',
        textField:'mc',
        panelHeight:'200',
        editable:true
    });

    //采购明细导出
    $("#btn_cgmx_export").click(function () {
        var cgdh = $('#cgdh').val() == null ? null : $('#cgdh').val();
        var gysId = $('#gys_jc').val() == null ? null : $('#gys_jc').val();//供应商的名称获取
        var ckId = $('#ck_mc').combotree('getValue') == null ? null : $('#ck_mc').combotree('getValue');//仓库的Id获取
        var code = $('#gg_code').val() == null ? null : $('#gg_code').val();
        var name = $('#gg_name').val() == null ? null : $('#gg_name').val();
        var ggxh = $('#gg_ggxh').val() == null ? null : $('#gg_ggxh').val();
        var cz = $('#gg_cz').val() == null ? null : $('#gg_cz').val();
        var sbgsId = $('#sbgs_jc').combotree('getValue') == null ? null : $('#sbgs_jc').combotree('getValue');//公司的Id获取
        var sbbmId = $('#sbbm_jc').combotree('getValue') == null ? null : $('#sbbm_jc').combotree('getValue');//部门的Id获取
        var zt = $("[name='cgmx_zt']").val() == null ? null : $("[name='cgmx_zt']").val();
        var fhrqStart = $('#cgmx_start').datebox('getValue');
        var fhrqEnd = $('#cgmx_end').datebox('getValue');
        window.open($.webPath() + "/cggl/cgmx/cgmx_export?cgdh=" + cgdh + "&&gysId=" + gysId
            + "&&ckId=" + ckId + "&&code=" + code + "&&name=" + name + "&&ggxh=" + ggxh + "&&cz=" + cz
            + "&&sbgsId=" + sbgsId + "&&sbbmId=" + sbbmId + "&&zt=" + zt + "&&fhrqStart=" + fhrqStart + "&&fhrqEnd=" + fhrqEnd);
    });

    //采购明细导出
    $("#btn_cgmx_tagexport").click(function () {
        var datas = $('#grid_cgmx').datagrid("getChecked");
        if (datas.length == 0) {
            alert("请先选择采购明细！");
            return;
        }
        var idArr = new Array();
        $(datas).each(function (i, data) {
            idArr.push("cgdmxIds=" + data.id);
        });

        window.open($.webPath() + "/cggl/cgmx/tag_export?" + idArr.join("&"));
    });
});

//实时库存查询
function cgmx_search() {
    var gysId = $('#gys_jc').val() == null ? null : $('#gys_jc').val();//供应商的名称获取
    var ckId = $('#ck_mc').combotree('getValue') == null ? null : $('#ck_mc').combotree('getValue');//仓库的Id获取
    var sbgsId = $('#sbgs_jc').combotree('getValue') == null ? null : $('#sbgs_jc').combotree('getValue');//公司的Id获取
    var sbbmId = $('#sbbm_jc').combotree('getValue') == null ? null : $('#sbbm_jc').combotree('getValue');//部门的Id获取
    $('#grid_cgmx').datagrid('load',
        {
            cgdh:$('#cgdh').val(),
            gysId:gysId,
            ckId:ckId,
            code:$('#gg_code').val(),
            name:$('#gg_name').val(),
            ggxh:$('#gg_ggxh').val(),
            cz:$('#gg_cz').val(),
            sbgsId:sbgsId,
            sbbmId:sbbmId,
            zt:$("[name='cgmx_zt']").val(),
            "fhrqStart":$('#cgmx_start').datebox('getValue'),
            "fhrqEnd":$('#cgmx_end').datebox('getValue')
        }
    );
}



